<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>组件通信</title>
</head>
<body>
<div id="app">
   {{message}}
    <component-a></component-a>
    <child-component>
        <h2 slot="header">标题</h2>
        <p>正文内容</p>
        <p>更多的正文内容</p>
        <div slot="footer">底部信息</div>
    </child-component>
</div>
<script src="https://unpkg.com/vue/dist/vue.min.js"></script>
<script>
    var bus = new Vue();
    Vue.component('component-a',{
        template: '<button @click="handleEvent">传递事件</button>',
        data:function (){
            return {
                counter: 0
            }
        },
        methods: {
            handleEvent: function (){
                bus.$emit('on-message','来自组件component-a的内容');
            }
        }
    });
    Vue.component('child-component',{
        template: '\
        \<div class="container">\
        <div class="header">\
        \<slot name="header"></slot>\
        </div>\
        \<div class="main">\
        <slot></slot></div>\
        \<div class="footer"><slot name="footer"></slot></div>\
        </div>\
        '
    });

    var app = new Vue({
        el: '#app',
        data:{
            message: ''
        },
       mounted: function (){
            var _this = this;
            bus.$on('on-message',function (msg){
               _this.message = msg;
            });
       }
    });
</script>
</body>
</html>
